<?php

namespace App\Kuafu\WaiMai\Command;

use App\Kuafu\Base\Model\BaseModel;
use App\Kuafu\WaiMai\Channel\BaseWaiMai;
use App\Kuafu\WaiMai\Enum\WaiMaiDBEnum;
use App\Kuafu\WaiMai\Model\WaiMaiCallbackLogModel;
use App\Kuafu\WaiMai\Model\WaiMaiOrderDetailModel;
use App\Kuafu\WaiMai\Model\WaiMaiOrderModel;
use App\Kuafu\WaiMai\Model\WaiMaiShopPromiseSwitchModel;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Illuminate\Console\Command;

class WaiMaiRefreshPromiseTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'waimai:refresh-promise-time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '外卖订单-同步承诺时间';

    /**
     * Create a new command instance.
     *
     * @return void//
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 数据校验的异常信息
     * @var array
     */
    private array $errs = [];

    /**
     * Execute the console command
     *
     * @return int
     */
    public function handle()
    {
        if (config('app.env') != 'prod') {
            dd('生产环境执行就行了');
        }

        WaiMaiShopPromiseSwitchModel::query()
                                    ->where('elmPromiseSwitch', WaiMaiDBEnum::SWITCH_ON)
                                    ->get()
                                    ->each(function ($item) {
                                        $wm = BaseWaiMai::getIns(WaiMaiDBEnum::SOURCE_TYPE_ELM, $item->shopId)
                                                        ->finishTimeConfig();

                                        if (!$wm['status'] || !isset($wm['data']['promiseTime'])) {
                                            return;
                                        }

                                        if ($wm['data']['promiseTime'] == $item->elmPromiseTime) {
                                            return;
                                        }


                                        $item->elmPromiseTime = $wm['data']['promiseTime'];
                                        $item->save();

                                        $this->info("WaiMaiRefreshPromiseTime shopId: {$item->shopId} elmPromiseTime: {$item->elmPromiseTime}");
                                    });
    }
}
